<script setup lang="ts">
import { getHomeGuessLikeApi } from '@/apis/home';
import type { PageParams } from '@/types/Global';
import type { SkuInfo } from '@/types/SkuModule';
import { ref, onMounted } from'vue';

// 参数
const pageParams:Required<PageParams> = {
  // 页码
  pageNum:1,
}

// 商品列表
const skuInfoList = ref<SkuInfo[]>([])
// 结束标识
const finish = ref(2);

// 获取数据
const getHomeGuessLike = async() => {
  // 退出分页判断
  if(finish.value === 1) {
    // return uni.showToast({ icon:'none', title:'暂无更多数据'})
    return
  }
  const res = await getHomeGuessLikeApi(pageParams)
  // 追加数据
  skuInfoList.value.push(...res.data.skus);
  // 页码追加
  if(pageParams.pageNum < res.data.pages) {
    pageParams.pageNum++;
    finish.value = 2;
  }else {
    finish.value = 1;
  }
}

// 重置数据
const refreshData = async() => {
  pageParams.pageNum = 1;
  skuInfoList.value = [];
  finish.value = 2;
  await getHomeGuessLike()
}

// 方法导出给外部使用
defineExpose({
  refreshData,
  getMore: getHomeGuessLike,
})

// 组件挂载好
onMounted(() => {
  getHomeGuessLike();
})
</script>

<template>
  <view class="caption">
    <text class="text">商品列表</text>
    <!-- <text class="text">猜你喜欢</text> -->
  </view>
  <view class="guess">
    <navigator
      class="guess-item"
      v-for="item in skuInfoList"
      :key="item.skuId"
      :url="`/pages/goods/goods?skuId=${item.skuId}`"
    >
      <image
        class="image"
        mode="aspectFill"
        :src="item.skuDefaultImg"
      ></image>
      <view class="name">{{ item.spuTitle }}</view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{ item.price }}</text>
      </view>
    </navigator>
  </view>
  <view v-if="!finish" class="loading-text"> 正在加载... </view>
  <view v-else class="loading-text"> 暂无更多数据 </view>
</template>

<style lang="scss">
page {
  display: block;
}
/* 分类标题 */
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  padding: 36rpx 0 40rpx;
  font-size: 28rpx;
  color: #262626;
  .text {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;

    &::before,
    &::after {
      content: '';
      width: 190rpx;
      height: 1px;
      background: linear-gradient(to right, transparent, red, transparent);
      margin: 0 10rpx;
    }
  }
}

/* 猜你喜欢 */
.guess{
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx;
  margin-bottom: 10rpx;
  .guess-item {
    width: 345rpx;
    padding: 24rpx 20rpx 20rpx;
    margin-bottom: 20rpx;
    border-radius: 10rpx;
    overflow: hidden;
    background-color: #fff;
  }
  .image {
    width: 304rpx;
    height: 304rpx;
  }
  .name {
    height: 75rpx;
    margin: 10rpx 0;
    font-size: 26rpx;
    color: #262626;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .price {
    line-height: 1;
    padding-top: 4rpx;
    color: #cf4444;
    font-size: 28rpx;
  }
  .small {
    font-size: 80%;
  }
}
// 加载提示文字
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}
</style>